import { loginAPI, getUserInfoApi, getUserDetailByIdApi } from '@/api/user'
import { setToken, getToken, removeToken } from '@/utils/auth'

const state = {
  token: getToken() || '',
  userInfo: {}
}
const mutations = {
  setToken (state, val) {
    state.token = val
  },
  changeUserInfo (state, val) {
    state.userInfo = val
  },
  goOut (state) {
    state.token = removeToken()
    state.userInfo = {}
  }
}
const actions = {
  async login (store, val) {
    const res = await loginAPI(val)
    store.commit('setToken', res.data)
    setToken(res.data)
    return res
  },
  async changeUserInfo (store) {
    const { data: res } = await getUserInfoApi()
    // console.log(res)
    const { data: data1 } = await getUserDetailByIdApi(res.userId)
    // console.log(data)
    const baseResult = {
      ...res,
      ...data1
    }
    store.commit('changeUserInfo', baseResult)
    // console.log(state.userInfo)
    return baseResult
  }
}
const getters = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
